/*
 * Created by SharpDevelop.
 * User: agsd
 * Date: 09/02/2008
 * Time: 16:23
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Calendar;

namespace Mogocal.service
{
	/// <summary>
	/// Description of EventRetriever.
	/// </summary>
	public class EventRetriever
	{
        public EventRetriever(ConfigurationSettings conf)
		{
            Conf = conf;
		}

        private CalendarService service = new CalendarService("CalendarSampleApp");
        private EventQuery query = new EventQuery();
        private ConfigurationSettings conf;
		
        public ConfigurationSettings Conf
        {
            get { return conf; }
            set { conf = value; }
        }

		public void GetEvents()
		{
            service.setUserCredentials(conf.GoogleAccount, conf.GoogleAccountPassword);
			Console.Out.WriteLine(conf.GoogleAccount + " "+conf.GoogleAccountPassword);
            //if (conf.ProxyEnabled)
            //{           	
            //        System.Net.WebProxy proxy = new System.Net.WebProxy(conf.ProxyHost,conf.ProxyPort);       
            //        proxy.Credentials = new System.Net.NetworkCredential(conf.ProxyUser, conf.ProxyPassword);
            //        //service.Credentials = proxy;                    
            //}
                 
            
            
            query.Uri = new Uri("http://www.google.com/calendar/feeds/default/private/full");

            query.StartTime = DateTime.Now.AddDays(-1);
            query.EndTime = DateTime.Now.AddDays(1);
			
            EventFeed calFeed = service.Query(query) as EventFeed;
            
            foreach (Google.GData.Calendar.EventEntry entry in calFeed.Entries)
            {
            	
               	foreach (When when in entry.Times)
            	{
               		foreach (Reminder rem in when.Reminders)
               		{
               			DateTime dt = when.StartTime.AddMinutes(- rem.Minutes);
               			dt.AddHours(- rem.Hours);
               			dt.AddDays( - rem.Days);
               			
               			if (dt < DateTime.Now.AddMinutes(1) && dt > DateTime.Now.AddMinutes(-1))
               			{
               				new EventNotifier().Notify(entry);
               			}
               		}        
            	}
            }
       }
	}
}
